package com.yike.user.controller;

import com.yike.common.pagination.Paging;
import com.yike.common.tool.LoginUtil;
import com.yike.common.vo.LoginUserInfoVo;
import com.yike.user.param.*;
import com.yike.user.service.UserService;
import com.yike.user.vo.UserFileVo;
import com.yike.user.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户表 控制器
 *
 * @author yike
 * @since 2024-07-17
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Module("user")
@Api(value = "用户管理API", tags = {"用户管理"})
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/register")
    @OperationLog(name = "用户注册", type = OperationLogType.ADD)
    @ApiOperation(value = "用户注册", response = ApiResult.class)
    public ApiResult<Boolean> register(@Validated @RequestBody UserRegisterParam userRegisterParam) throws Exception {
        boolean flag = userService.register(userRegisterParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    @OperationLog(name = "修改用户", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改用户", response = ApiResult.class)
    public ApiResult<Boolean> updateUser(@Validated @RequestBody UserUpdateParam userUpdateParam) throws Exception {
        boolean flag = userService.updateUser(userUpdateParam);
        return ApiResult.result(flag);
    }

    /**
     * 用户登录
     */
    @PostMapping("/updatePassword")
    @OperationLog(name = "修改密码", type = OperationLogType.ADD)
    @ApiOperation(value = "修改密码", response = ApiResult.class)
    public ApiResult<Boolean> updatePassword(@Validated @RequestBody UpdatePasswordParam updatePasswordParam) throws Exception {
        boolean flag = userService.updatePassword(updatePasswordParam);
        return ApiResult.result(flag);
    }

    /**
     * 用户表分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "用户分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "用户分页列表", response = UserVo.class)
    public ApiResult<Paging<UserVo>> getUserPageList(@Validated @RequestBody UserPageParam userPageParam) throws Exception {
        Paging<UserVo> paging = userService.getUserPageList(userPageParam);
        return ApiResult.ok(paging);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @OperationLog(name = "商户登录", type = OperationLogType.LOGIN)
    @ApiOperation(value = "商户登录", response = LoginUserInfoVo.class)
    public ApiResult<LoginUserInfoVo> login(@Validated @RequestBody LoginParam loginParam) throws Exception {
        LoginUserInfoVo loginUserInfoVo = userService.login(loginParam);
        return ApiResult.ok(loginUserInfoVo);
    }

    /**
     * 用户登录
     */
    @PostMapping("/logout")
    @OperationLog(name = "商户登出", type = OperationLogType.LOGOUT)
    @ApiOperation(value = "商户登出")
    public ApiResult logout() {
        LoginUtil.logout();
        return ApiResult.ok();
    }

    @GetMapping("/info")
    //@OperationLog(name = "用户详情", type = OperationLogType.LOGIN)
    @ApiOperation(value = "用户详情", response = UserVo.class)
    public ApiResult<UserVo> info() throws Exception {
        UserVo userVo = userService.info();
        return ApiResult.ok(userVo);
    }

    @GetMapping("/getUserHeadImgList")
    //@OperationLog(name = "获取用户头像列表", type = OperationLogType.LOGIN)
    @ApiOperation(value = "获取用户头像列表", response = UserFileVo.class)
    public ApiResult<List<UserFileVo>> getUserHeadImgList() throws Exception {
        List<UserFileVo> userFileVos = userService.getUserHeadImgList();
        return ApiResult.ok(userFileVos);
    }


    @PostMapping("/appMsgSendSetting")
    @OperationLog(name = "APP消息推送设置", type = OperationLogType.LOGIN)
    @ApiOperation(value = "APP消息推送设置", response = ApiResult.class)
    public ApiResult<Boolean> appMsgSendSetting(@Validated @RequestBody UserSettingParam userSettingParam) throws Exception {
        boolean flag = userService.appMsgSendSetting(userSettingParam);
        return ApiResult.result(flag);
    }

}

